package com.solo.servlet;

import com.solo.entity.Category;
import com.solo.entity.Category;
import com.solo.service.CategoryService;
import com.solo.service.CategoryService;
import com.solo.service.impl.CategoryServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CategoryServlet", value = "/CategoryServlet")
public class CategoryServlet extends HttpServlet {
    private CategoryService categoryService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取对应的请求参数
        String action = request.getParameter("method");
        System.out.println("action====" + action);
        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "add":
                    addCategory(request, response);
                    break;
                case "delete":
                    deleteCategory(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "update":
                    updateCategory(request, response);
                    break;
                default:
                    listCategory(request, response);
                    break;
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/category/addCategory.jsp");
        dispatcher.forward(request, response);
    }


    private void addCategory(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = request.getParameter("name");
        String orderNum = request.getParameter("orderNum");

        Category category = new Category(name, orderNum);
        categoryService = new CategoryServiceImpl();
        categoryService.addCategory(category);
        // response.sendRedirect("user.jsp");
        listCategory(request, response);
    }

    private void deleteCategory(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("deleteCategory====");

        Long id = Long.parseLong(request.getParameter("id"));
        System.out.println("id====" + id);
        Category category = new Category(id);
        categoryService = new CategoryServiceImpl();
        categoryService.deleteCategory(category);
        listCategory(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        System.out.println("id====" + id);
        categoryService = new CategoryServiceImpl();
        Category category = categoryService.selectCategoryById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/category/editCategory.jsp");
        request.setAttribute("category", category);
        dispatcher.forward(request, response);
    }
    private void updateCategory(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        String orderNum = request.getParameter("orderNum");

        Category category = new Category(id, name, orderNum);

        categoryService = new CategoryServiceImpl();
        categoryService.updateCategory(category);
        listCategory(request, response);
    }

    private void listCategory(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // response.setContentType("text/html;charset=utf-8");
        categoryService = new CategoryServiceImpl();
        List<Category> listCategory = categoryService.selectAllCategory();
        request.setAttribute("categoryList", listCategory);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/category/category.jsp");
        dispatcher.forward(request, response);
    }
}